# -*- coding:utf-8 -*-
from fastapi import APIRouter, Request, Depends, Query
from typing import Union
from core.curd import PageInfo, IDList
from core.request import get_page_info, get_query_param
from core.response import tableinfo, success
from app.annotation import RequiresPermissions
from app.service.system import SysPostService
from app.entity.system import CreateSysPost, UpdateSysPost

router = APIRouter()


@router.get('/get')
@RequiresPermissions('sys:post:detail')
async def get_one(ids: int = Query(), mService=Depends(SysPostService.instance)):
    """
        获取测试详情
    """
    one = await mService.select_by_id(ids)
    return success('OK', one)


@router.get('/selectpage')
async def select_options(mService=Depends(SysPostService.instance)):
    data = await mService.select_options()
    return success("OK", data)


@router.get('/list')
@RequiresPermissions('sys:post:list')
async def lists(req: Request, page=Depends(get_page_info), where=Depends(get_query_param),
               mService=Depends(SysPostService.instance)):
    """
        获取测试列表
    """
    total, rows = await mService.select_page(page, where)
    return tableinfo('OK', total, rows)


@router.post('/add')
@RequiresPermissions('sys:post:add')
async def add(post: CreateSysPost, mService=Depends(SysPostService.instance)):
    """
        添加测试
    """
    one = await mService.insert(post.dict())
    return success('OK', one)


@router.post('/edit')
@RequiresPermissions('sys:post:edit')
async def update(post: UpdateSysPost, mService=Depends(SysPostService.instance)):
    """
        编辑测试
    """
    one = await mService.update(post.dict())
    return success('OK', one)


@router.post('/del')
@RequiresPermissions('sys:post:remove')
async def remove(ids: IDList, mService=Depends(SysPostService.instance)):
    """
        删除测试
    """
    one = await mService.delete_batch_ids(ids.ids)
    return success('OK')


@router.post('/import')
@RequiresPermissions('sys:post:import')
async def imports(req: Request, mService=Depends(SysPostService.instance)):
    """
        导入测试列表
    """

    return success('OK')


@router.post('/export')
@RequiresPermissions('sys:post:export')
async def export(req: Request, mService=Depends(SysPostService.instance)):
    """
        导出测试列表
    """

    return success('OK')
